using System;
using System.IO;

namespace Seagull.FrameInspector.Seagull.DAL
{
    public static class TimestampAccessor
    {
        public static DateTime Read(BinaryReader reader)
        {
            // BUG: Possible broken in 2100; same below
            var year = reader.ReadByte() + 2000;
            var month = reader.ReadByte();
            var day = reader.ReadByte();
            var hour = reader.ReadByte();
            var minute = reader.ReadByte();
            var second = reader.ReadByte();
            var hundreduS = reader.ReadInt16();
            var timestamp = new DateTime(year, month, day, hour, minute, second).AddMilliseconds(hundreduS / 10.0);
            return timestamp;
        }

        public static void Write(BinaryWriter writer, DateTime timestamp)
        {
            Action<int> writeByte = i => writer.Write((byte)i);
            writeByte(timestamp.Year - 2000);
            writeByte(timestamp.Month);
            writeByte(timestamp.Day);
            writeByte(timestamp.Hour);
            writeByte(timestamp.Minute);
            writeByte(timestamp.Second);
            writer.Write((Int16)(timestamp.Ticks % 10000000L / 1000));
        }
    }
}